/**
 * 商品备案申请子件表单js
 */

/**
 * 表单自定义控制器
 */
app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http) {
	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增
			{
				var rowCurrent = {};
				rowCurrent.id = null;
				rowCurrent.pcode = $scope.valueMap['code'];
				rowCurrent.code = '保存后自动生成';
				rowCurrent.name = '';
				rowCurrent.num = 1;
				rowCurrent.bprice = 1;
				rowCurrent.linkIn = "";
				rowCurrent.linkInMark = "";
				rowCurrent.remark = "";
				$scope.subGrid.sonlist.push(rowCurrent);

				var priceIn = 0;
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					var num = parseFloat($scope.subGrid.sonlist[i]['num']);
					var bprice = parseFloat($scope.subGrid.sonlist[i]['bprice']);
					priceIn = priceIn + (num * bprice);
				}
				$scope.valueMap['priceIn'] = $scope.toFixed(priceIn,2);
				break;
			}
			case 'event-to-delete': // 删除
			{
				var datas = [];
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					var data = $scope.subGrid.sonlist[i];
					if(data['ys_form_sonlist_checkbox'] == true) {
						continue;
					} else {
						datas.push(data);
					}
				}
				if (datasDel.length != 0){
					$http.post('/api/inter/tpProductBqSp/del', {}, {params: {list: datasDel,code:$scope.valueMap['code']}})
						.success(function (response) {
								if(response.code == 1) {
									layer.closeAll();
									return layer.msg("删除失败，请稍后再试！", {icon: 2});
								}
							}
						);
				}
				$scope.subGrid.sonlist = datas;
				layer.msg("删除成功!", {icon: 2});
				break;
			}
		};
	}
}

/**
 * 表单自定义控制器
 */
app.controller('formSelfCtrl', ['$scope', '$http', function FormSelfController($scope, $http){

	/*
	 * 数字框变动
	 */
	$scope.subGrid.numberChange = function(row, field) {
		if(field == 'num' || field == 'bprice'){
			// row.num = $scope.toFixed(row.num,2)
			// row.bprice = $scope.toFixed(row.bprice,2)
		}
		var priceIn = 0;
		for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
			var num = parseFloat($scope.subGrid.sonlist[i]['num']);
			var bprice = parseFloat($scope.subGrid.sonlist[i]['bprice']);
			priceIn = priceIn + (num * bprice);
		}
		$scope.valueMap['priceIn'] = $scope.toFixed(priceIn,2);
	}

}]);
